<!DOCTYPE html>
<title>Check that crossorigin media requests don't reveal information about non-media files.</title>
<script src="/w3c/resources/testharness.js"></script>
<script src="/w3c/resources/testharnessreport.js"></script>
<script src="/media-resources/media-file.js"></script>
<video id=video1></video>
<video id=video2 preload=none></video>
<script>

promise_test(async function() {
  function get_all_events(url) {
    return new Promise(function(resolve, reject) {
      let events = [];
      let current_state = -1;
      let current_buffered_string = "";
      const video = document.querySelector("#video1");
      function pollTagState() {
        const state = video.networkState;
        if (state != current_state) {
          current_state = state
          events.push("NetworkState=" + state);
        }
        const buffered = video.buffered;
        let buffered_string = "";
        for (let i = 0; i < buffered.length; i++) {
          buffered_string += "(" + buffered.start(i) + "-" + buffered.end(i) + ")";
        }
        if (buffered_string != current_buffered_string) {
          current_buffered_string = buffered_string;
          events.push("Buffered=" + buffered_string);
        }
      }

      for (var prop in video) {
        if (prop.slice(0,2) == "on") {
          video[prop] = function(e) {
            pollTagState()
            events.push(e.type);
          }
        }
      }
      pollTagState();
      const interval = setInterval(function() { pollTagState() }, 1);
      video.onerror = function(e) {
        pollTagState()
        events.push("Error("+video.error.message+")");
        // Wait for network state to stabilize.
        setTimeout(function() {
          clearInterval(interval);
          resolve(events);
        }, 100);
      };
      video.src = url;
      video.play().catch(e=>0);
    });
  }

  const nonexistant_remote = "http://localhost:8000/media/nonexistant.cgi";
  const existant_remote = "http://localhost:8000/media/video-throttled-load.cgi?name=resources/test.txt&throttle=200&type=text/plain";
  // First do a warmup run. Switching between sources adds some events, so
  // the first run will be slightly different.
  await get_all_events(nonexistant_remote);

  // Get events for a nonexistant remote resource.
  const nonexisting_events = await get_all_events(nonexistant_remote);

  // Get events for a existant remote resource.
  const existing_events = await get_all_events(existant_remote);

  console.log(existing_events.join(","));
  console.log(nonexisting_events.join(","));
  assert_equals(existing_events.join(","), nonexisting_events.join(","));
});

promise_test(async function() {
    return new Promise(function(resolve, reject) {
      const video = document.querySelector("#video2");
      video.src = "http://localhost:8000/media/nonexistant.cgi";
      function checkNetworkState() {
        if (video.networkState == video.NETWORK_IDLE) resolve();
      }
      setInterval(checkNetworkState, 100);
    });
});

</script>
